from django.db import models
# Create your models here.
from users.models import User


class BaseModel(models.Model):
    """为模型类补充字段"""
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    is_delete = models.BooleanField(default=False, verbose_name='是否删除')

    class Meta:
        abstract = True  # 说明是抽象模型类, 用于继承使用，数据库迁移时不会创建BaseModel的表


class GoodsCategory(BaseModel):
    """商品类别 """
    name = models.CharField(max_length=30, verbose_name='类别名', help_text='类别名')
    image = models.ImageField(default='', verbose_name="类目图片")
    is_index = models.BooleanField(default=False, verbose_name='是否首页显示')

    class Meta:
        db_table = 'tb_goods_category'
        verbose_name = '商品类别'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Brand(BaseModel):
    """品牌"""
    name = models.CharField(max_length=20, verbose_name='名称')

    class Meta:
        db_table = 'tb_brand'
        verbose_name = '品牌'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Goods(BaseModel):
    """商品"""
    GOODS_STATUS = (
        (1, "上架"),
        (0, "下架"),
    )
    category = models.ForeignKey(GoodsCategory, on_delete=models.CASCADE, verbose_name='商品类别', help_text='商品类别')
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE, verbose_name='品牌', help_text="品牌")
    goods_sn = models.CharField(max_length=50, default='', verbose_name='货号', help_text="货号")
    name = models.CharField(max_length=100, verbose_name='商品名', help_text='商品名')
    goods_front_image = models.ImageField(verbose_name="封面图", help_text="封面图")
    sold_num = models.IntegerField(default=0, null=True, blank=True, verbose_name='商品销售量', help_text="商品销售量")
    ship_address = models.CharField(max_length=20, default='广东', verbose_name='发货地址', help_text="发货地址")
    serve = models.CharField(max_length=100, default='正品保障 7天无忧退货', verbose_name='服务', help_text="服务")
    market_price = models.FloatField(default=0, verbose_name="传统零售价", help_text="传统零售价")
    shop_price = models.FloatField(default=0, verbose_name="本店价格", help_text="本店价格")
    goods_status = models.IntegerField(choices=GOODS_STATUS, default=1, verbose_name='商品状态', help_text="商品状态")
    can_repair = models.BooleanField(default=False, verbose_name='是否可维修', help_text="是否可维修")
    is_hot = models.BooleanField(default=False, verbose_name='是否热销', help_text='是否热销')
    is_new = models.BooleanField(default=True, verbose_name='是否新品', help_text='是否新品')
    is_highpoint = models.BooleanField(default=False, verbose_name='是否为高端', help_text='是否为高端')
    is_index = models.BooleanField(default=False, verbose_name='是否首页显示', help_text='是否首页显示')
    start_time = models.DateField(null=True, blank=True, verbose_name='上架时间')
    end_time = models.DateField(null=True, blank=True, verbose_name="下架时间")
    ship_free = models.BooleanField(default=True, verbose_name="是否承担运费", help_text="是否承担运费")
    views_amount = models.IntegerField(default=0, verbose_name='浏览量', help_text="浏览量", null=True)
    relevance_goods = models.ManyToManyField('self', null=True, blank=True, verbose_name='关联的商品推荐', help_text="关联的商品推荐")

    class Meta:
        db_table = 'tb_goods'
        verbose_name = '商品'
        verbose_name_plural = verbose_name
        ordering = ["id"]

    def __str__(self):
        return self.name


class GoodsAttribute(BaseModel):
    """商品属性"""
    goods_code = models.CharField(default='', max_length=50, verbose_name="商品编码")
    color = models.CharField(max_length=50, null=True, blank=True, verbose_name="颜色")
    image = models.ImageField(null=True, blank=True, verbose_name="规格对应图片")
    size = models.CharField(max_length=50, default='默认规格', null=True, blank=True, verbose_name="规格")
    weight = models.IntegerField(default=1, null=True, blank=True, verbose_name='重量')
    price = models.FloatField(default=0, verbose_name="售价")
    storage_amout = models.IntegerField(default=0, verbose_name='库存量')
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品", related_name="attribute")

    class Meta:
        db_table = 'tb_attribute'
        verbose_name = '商品属性'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name


class DiscountPackage(BaseModel):
    """优惠套餐"""
    name = models.CharField(max_length=100, verbose_name='套餐名', help_text='套餐名')
    image = models.ImageField(verbose_name="封面图片")
    saved_at_most = models.FloatField(default=0, verbose_name="套餐优惠立减价格")
    goods = models.ManyToManyField(Goods, verbose_name='商品')
    start_time = models.DateField(verbose_name='活动开始时间')
    end_time = models.DateField(verbose_name="活动截止时间")
    is_valid = models.BooleanField(default=False, verbose_name='是否有效')
    number = models.IntegerField(default=0, verbose_name='套餐使用量')

    class Meta:
        db_table = 'tb_discountpackage'
        verbose_name = '优惠套餐'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class GoodsBannerImage(BaseModel):
    """商品详情页轮播图"""
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品", related_name="banner_images")
    banner_image = models.ImageField(verbose_name="轮播图片", null=True, blank=True)

    class Meta:
        db_table = 'tb_goodsbannerimage'
        verbose_name = '商品详情页轮播图片'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name


class GoodsDescImage(BaseModel):
    """商品详情页详情图"""
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品", related_name="desc_images")
    desc_image = models.ImageField(verbose_name="详情图片", null=True, blank=True)

    class Meta:
        db_table = 'tb_goodsdescimage'
        verbose_name = '商品详情页详情图片'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name


class Banner(BaseModel):
    """首页轮播的商品图，为适配首页大图"""
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品")
    image = models.ImageField(verbose_name="轮播图片")

    class Meta:
        db_table = 'tb_banner'
        verbose_name = '首页轮播'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name


class HotSearchWords(models.Model):
    """搜索栏下方热搜词"""
    keywords = models.CharField(default="", max_length=20, verbose_name="热搜词")
    index = models.IntegerField(default=0, verbose_name="排序")

    class Meta:
        db_table = 'tb_hotsearchwords'
        verbose_name = '热搜排行'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.keywords


class SearchAd(BaseModel):
    """搜索框下方广告"""
    title = models.CharField(default="", max_length=50, verbose_name="活动标题")
    image = models.ImageField(verbose_name="精选活动轮播图片")

    class Meta:
        db_table = 'tb_searchad'
        verbose_name = '搜素框下方广告'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title
